package com.meirimoney.app;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.meirimoney.app.control.HeadControl;
import com.meirimoney.app.control.MenuBar;
import com.meirimoney.sdk.models.Commodity;
import com.meirimoney.task.CommodityTask;

/***
 * 兑换中心
 * 
 * @author gewei
 * 
 */
public class CommodityList extends BaseActivity implements OnItemClickListener,
		OnClickListener, android.content.DialogInterface.OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.initializeUI();
		this.setTitle("兑换中心");
		this.load();
	}

	/***
	 * 初始化当前界面的UI
	 */
	private void initializeUI() {
		this.setContentView(R.layout.mrm_commodity);
		if (null == this.backButton) {
			this.backButton = (TextView) this.findViewById(R.id.backButton);
			this.backButton.setOnClickListener(this);
		}
		if (null == this.titleView) {
			this.titleView = (TextView) this.findViewById(R.id.title);
		}
		if (null == this.guideButton) {
			this.guideButton = (Button) this.findViewById(R.id.guide);
			this.guideButton.setOnClickListener(this);
		}
		if (null == this.list) {
			this.list = (ListView) this.findViewById(R.id.commodityList);
			this.list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			this.list.setOnItemClickListener(this);
		}
	}

	private void load() {
		if (this.task != null) {
			AsyncTask.Status status = this.task.getStatus();
			if (status != AsyncTask.Status.FINISHED) {
				return;
			}
		}
		this.task = new CommodityTask(this, this.list);
		this.task.execute(0);
	}

	private ListView list;

	private CommodityTask task;

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		Adapter adapter = this.list.getAdapter();
		Commodity commodity = (Commodity) adapter.getItem(position);
		Intent i = null;
		if (commodity.Category == 1) {
			i = new Intent(this, PhoneRecharge.class);
		} else if (commodity.Category == 2) {
			i = new Intent(this, AlipayWithdraw.class);
		}
		i.putExtra("commodity", this.getBundle(commodity));
		this.startActivity(i);
	}

	/***
	 * 根据商品对象创建包裹对象
	 * 
	 * @param commodity
	 *            商品对象
	 * @return 包裹对象
	 */
	public Bundle getBundle(Commodity commodity) {
		Bundle bd = new Bundle();
		bd.putInt("category", commodity.Category);
		bd.putInt("integral", commodity.Integral);
		bd.putDouble("price", commodity.Price);
		bd.putLong("id", commodity.Id);
		return bd;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.backButton) {
			this.finish();
		} else if (v.getId() == R.id.guide) {
			if (null == this.guideView) {
				this.guideView = ((LayoutInflater) this
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
						.inflate(R.layout.mrm_guid_exchange, null);
			}
			if (null != this.dialog) {
				this.dialog.dismiss();
				this.dialog.show();
			} else {
				Builder builder = new Builder(this);
				builder.setTitle("积分兑换说明");
				builder.setView(this.guideView);
				builder.setPositiveButton("知道了", this);
				this.dialog = builder.create();
				dialog.show();
			}
		}
	}

	public void setTitle(String title) {
		this.titleView.setText(title);
	}

	private TextView backButton;

	private Button guideButton;

	private TextView titleView;

	private AlertDialog dialog;

	private View guideView;

	@Override
	public void onClick(DialogInterface dialog, int which) {
		dialog.dismiss();
	}
}
